﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesRpt_108 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "Departmentname";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", "0"));
                ddlCompany.SelectedValue = "0";

                

                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void setdrData(ref DataRow drnew, DataRow drsource)
        {
            drnew["rdSaleAmount1"] = drsource["rdSaleAmount1"];
            drnew["rdSaleAmount2"] = drsource["rdSaleAmount2"];
            drnew["rdSaleAmount3"] = drsource["rdSaleAmount3"];
            drnew["rdSaleAmount"] = drsource["rdSaleAmount"];
            drnew["kxSaleAmount1"] = drsource["kxSaleAmount1"];
            drnew["kxSaleAmount2"] = drsource["kxSaleAmount2"];
            drnew["kxSaleAmount3"] = drsource["kxSaleAmount3"];
            drnew["kxSaleAmount"] = drsource["kxSaleAmount"];
            drnew["yxSaleAmount1"] = drsource["yxSaleAmount1"];
            drnew["yxSaleAmount2"] = drsource["yxSaleAmount2"];
            drnew["yxSaleAmount3"] = drsource["yxSaleAmount3"];
            drnew["yxSaleAmount"] = drsource["yxSaleAmount"];
            drnew["hzSaleAmount1"] = drsource["hzSaleAmount1"];
            drnew["hzSaleAmount2"] = drsource["hzSaleAmount2"];
            drnew["hzSaleAmount3"] = drsource["hzSaleAmount3"];
            drnew["hzSaleAmount"] = drsource["hzSaleAmount"];
            drnew["hcSaleAmount1"] = drsource["hcSaleAmount1"];
            drnew["hcSaleAmount2"] = drsource["hcSaleAmount2"];
            drnew["hcSaleAmount3"] = drsource["hcSaleAmount3"];
            drnew["hcSaleAmount"] = drsource["hcSaleAmount"];
            drnew["TotalSaleAmount1"] = drsource["TotalSaleAmount1"];
            drnew["TotalSaleAmount2"] = drsource["TotalSaleAmount2"];
            drnew["TotalSaleAmount3"] = drsource["TotalSaleAmount3"];
            drnew["TotalSaleAmount"] = drsource["TotalSaleAmount"];
        }
        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = rvReporting;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath = strRootPath + "Report\\rdlc\\SalesRpt_108.rdlc";

            strFilePath = "report/rdlc/SalesRpt_108.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode   
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;

            //get dataset  
            BillBll bll = new BillBll();
            DataSet ds;
            ds = bll.SalesRpt_108(sqlWhere, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            ds.DataSetName = "ds1_vw_SalesRpt_108";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "ds1_vw_SalesRpt_108";

            DataTable dt = ds.Tables[0];
            DataTable dttmp;
            DataRow dr;
            DataRow drtmp;

            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_108(sqlWhere, "", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            if (dttmp.Rows.Count > 0)
            {
                drtmp = dttmp.Rows[0];
                dr["FactoryName"] = "900合计：";
                setdrData(ref dr, drtmp);
                dt.Rows.Add(dr);
            }
            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_108(sqlWhere, " and isOwner='1'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            if (dttmp.Rows.Count > 0)
            {
                drtmp = dttmp.Rows[0];
                dr["FactoryName"] = "901自产产品小计：";
                setdrData(ref dr, drtmp);
                dt.Rows.Add(dr);
            }
            dr = dt.NewRow();
            dttmp = bll.sumSalesRpt_108(sqlWhere, " and isOwner='0'", ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);
            if (dttmp.Rows.Count>0)
            {
                drtmp = dttmp.Rows[0];
                dr["FactoryName"] = "902代理产品小计：";
                setdrData(ref dr, drtmp);
                dt.Rows.Add(dr);
            }
            
            rds.Value = dt;
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportViewer.LocalReport.Refresh();
            
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" and ((companyname in(").Append("select DepartmentName from cms_sys_Department where iscompany = 1 and DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("'))");
            sb.Append(" and CategoryCode in(").Append("select categoryCode from Cms_Ctrt_InstrumentCategory where categoryGUID in(select categoryGUID from Cms_sys_UserProductCategory where UserID ='").Append(base.CurrentUserID.ToString()).Append("')))");
            sb.Append(" or sales ='").Append(base.CurrentUserEntity.UserName.ToString()).Append("'");
            sb.Append(" or sales in(").Append("select username from vw_sysUser where DepartmentID in(select DepartmentID from Cms_Sys_UserDepartment_DataRight where userid ='").Append(base.CurrentUserID.ToString()).Append("')))");
           
           
            if (ddlCompany.SelectedValue != "0")
            {
                sb.Append(" and companyname = '" + ddlCompany.SelectedValue.ToString() + "'");
            }
            

            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        
    }
}
